﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
/// <summary>
/// Summary description for SecureModule
/// </summary>
public class SecureModule : IHttpModule
{

    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;
        HttpContext context = application.Context;
        HttpRequest request = application.Context.Request;
        HttpResponse response = application.Context.Response;

        try
        {
            string decryptedUrl = new Helper().DecryptUrl(request.Path.TrimStart('/'));
            application.Context.RewritePath(decryptedUrl.TrimStart('/'));
        }
        catch (Exception ex)
        {
            //application.Context.Response.Write("Encrypted Url not provided..");
            //application.CompleteRequest();
        }
        //application.CompleteRequest(application.Context.Request.Path);
    }
}

public class Helper
{
    MD5CryptoServiceProvider _provider = new MD5CryptoServiceProvider();
    byte[] input;
    byte[] output;

    public string EncryptUrl(string url)
    {
        RijndaelManaged managed = new RijndaelManaged();
        managed.GenerateKey();
        ICryptoTransform encryptor = managed.CreateEncryptor();
        input = System.Text.Encoding.ASCII.GetBytes(url);
        //output = _provider.ComputeHash(input, 0, input.Length);
        //return System.Text.Encoding.ASCII.GetString(output);
        return Convert.ToBase64String(input);
    }

    public string DecryptUrl(string url)
    {
        output = Convert.FromBase64String(url);
        return System.Text.Encoding.ASCII.GetString(output);
    }

}